package com.example.wms_supermarket.service.impl;


import com.example.wms_supermarket.Util;
import com.example.wms_supermarket.entity.Goods;
import com.example.wms_supermarket.entity.Message;
import com.example.wms_supermarket.entity.OutStockTable;
import com.example.wms_supermarket.entity.User;
import com.example.wms_supermarket.mapper.OutStockTableMapper;
import com.example.wms_supermarket.mapper.UserMapper;
import com.example.wms_supermarket.service.OutStockTableService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OutStockTableServiceImpl implements OutStockTableService {
    @Autowired
    private OutStockTableMapper outStockTableMapper;
    @Autowired
    private UserMapper userMapper;
    private Message oauth(HttpServletRequest httpServletRequest){
        Message<List<Goods>> msg=new Message<>();
        Cookie[] cookies=httpServletRequest.getCookies();
        String id= Util.getCookieValue(cookies,"userId");
        if (id==null){
            msg.setCode(102);
            msg.setMsg("当前身份无权限查看");
            return msg;
        }
        User user =userMapper.queryById(Integer.parseInt(id));
        if(user==null){
            msg.setCode(103);
            msg.setMsg("登录信息有误");
        }
        else if (!user.getRole().equals("2") && !user.getRole().equals("1")){
            msg.setCode(102);
            msg.setMsg("当前身份无权限查看");
        }
        else{
            msg.setCode(0);
            msg.setMsg("可以进行查询");
        }
        return msg;
    }
    @Override
    public Message<List<OutStockTable>> queryAll(HttpServletRequest httpServletRequest) {
        Message<List<OutStockTable>> msg=new Message<>();
        Message oauthMsg=oauth(httpServletRequest);
        if (oauthMsg.getCode()!=0){
            msg.setCode(oauthMsg.getCode());
            msg.setMsg(oauthMsg.getMsg());
        }
        else{
            msg.setCode(0);
            msg.setMsg("查询成功");
            msg.setData(outStockTableMapper.queryAll());
        }
        return msg;
    }

    @Override
    public Message<OutStockTable> queryByTid(HttpServletRequest httpServletRequest) {
        Message <OutStockTable> msg=new Message<>();
        Message oauthMsg=oauth(httpServletRequest);
        if (oauthMsg.getCode()!=0){
            msg.setCode(oauthMsg.getCode());
            msg.setMsg(oauthMsg.getMsg());
        }
        else{
            String tid=httpServletRequest.getParameter("tid");
            if (tid==null){
                msg.setCode(105);
                msg.setMsg("传入参数错误");
                return msg;
            }
            OutStockTable outStockTable=outStockTableMapper.queryByTid(Integer.parseInt(tid));
            if (oauthMsg!=null){
                msg.setCode(0);
                msg.setMsg("查询成功");
                msg.setData(outStockTable);
            }
            else{
                msg.setCode(1);
                msg.setMsg("查询失败");
            }

        }
        return msg;
    }

    @Override
    public Message<List<OutStockTable>> queryByGid(HttpServletRequest httpServletRequest) {
        Message <List<OutStockTable>> msg=new Message<>();
        Message oauthMsg=oauth(httpServletRequest);
        if (oauthMsg.getCode()!=0){
            msg.setCode(oauthMsg.getCode());
            msg.setMsg(oauthMsg.getMsg());
        }
        else{
            String gid=httpServletRequest.getParameter("gid");
            if (gid==null){
                msg.setCode(105);
                msg.setMsg("传入参数错误");
                return msg;
            }
            msg.setCode(0);
            msg.setMsg("查询成功");
            msg.setData(outStockTableMapper.queryByGid(Integer.parseInt(gid)));
        }
        return msg;
    }

    @Override
    public Message<List<OutStockTable>> queryByStatus(HttpServletRequest httpServletRequest) {
        Message <List<OutStockTable>> msg=new Message<>();
        Message oauthMsg=oauth(httpServletRequest);
        if (oauthMsg.getCode()!=0){
            msg.setCode(oauthMsg.getCode());
            msg.setMsg(oauthMsg.getMsg());
        }
        else{
            String status=httpServletRequest.getParameter("status");
            if (status==null){
                msg.setCode(105);
                msg.setMsg("传入参数错误");
                return msg;
            }
            msg.setCode(0);
            msg.setMsg("查询成功");
            msg.setData(outStockTableMapper.queryByStatus(Integer.parseInt(status)));
        }
        return msg;
    }

    @Override
    public Message insertOutTable(HttpServletRequest httpServletRequest) {
        Message msg=new Message<>();
        Message oauthMsg=oauth(httpServletRequest);
        if (oauthMsg.getCode()!=0){
            msg.setCode(oauthMsg.getCode());
            msg.setMsg(oauthMsg.getMsg());
        }
        else{
            int tid=Util.generateId();
            String gid=httpServletRequest.getParameter("gid");
            if (gid==null){
                msg.setCode(105);
                msg.setMsg("传入参数错误");
                return msg;
            }
            msg.setCode(0);
            msg.setMsg("插入成功");
            outStockTableMapper.insertOutTable(tid,Integer.parseInt(gid));
        }
        return msg;
    }

    @Override
    public Message grantOutTable(HttpServletRequest httpServletRequest) {
        Message msg=new Message<>();
        Message oauthMsg=oauth(httpServletRequest);
        if (oauthMsg.getCode()!=0){
            msg.setCode(oauthMsg.getCode());
            msg.setMsg(oauthMsg.getMsg());
        }
        else{
            String tid=httpServletRequest.getParameter("tid");
            if (tid==null){
                msg.setCode(105);
                msg.setMsg("传入参数错误");
                return msg;
            }
            msg.setCode(0);
            msg.setMsg("插入成功");
            outStockTableMapper.grantOutTable(Integer.parseInt(tid));
        }
        return msg;

    }

    @Override
    public Message completeOutTable(int tid) {
        Message msg=new Message();
        msg.setCode(0);
        msg.setMsg("货物出库");
        outStockTableMapper.completeOutTable(tid);
        return msg;
    }
}
